using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using DataBase.Core;

namespace MeetFuture.MRC
{
    public partial class DataBaseHelper_MRC
    {
        private DataBaseLayer xdb;

        public DataBaseHelper_MRC()
        {
            xdb = new DataBaseLayer(ApplicationGlobal.ConnectString_MRC, DataBaseConstant.DataBase_Type.PostgreSQL);
        }

        public DataBaseLayer GetDataBase
        {
            get { return this.xdb; }
        }

        public void Update_Mission_BatchInfo(clsBatchInfo BatchInfo)
        {
            try
            {
            	if (!Is_Exist_BatchInfo(BatchInfo))
            	{
                    Insert_BatchInfo(BatchInfo);
                    foreach (clsLotInfo Lot in BatchInfo.Lots)
                    {
                        Insert_LotInfo(Lot);
                    }
            	} 
            	else
            	{
                    Update_BatchInfo(BatchInfo);
            	}
            }
            catch (System.Exception ex)
            {	
            }
        }

        public void Insert_BatchInfo(clsBatchInfo BatchInfo)
        {
            try
            {
                DBCommand cmd = new DBCommand();
                cmd.CommandText = @"insert into tb_agv_jobinfo (agv_id, job_id, job_start, job_end) values (:agv_id, :job_id, :job_start, :job_end) ";
                cmd.AddParameter(":agv_id", BatchInfo.AGV);
                foreach (clsLotInfo lot in BatchInfo.Lots)
                {
                    cmd.AddParameter(":job_id", lot.Mission);
                    break;
                }
                cmd.AddParameter(":job_start", BatchInfo.StartTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                cmd.AddParameter(":job_end", BatchInfo.EndTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                int result = xdb.ExecuteSql(cmd.CommandText);
            }
            catch (System.Exception ex)
            {	
            }
        }

        public void Update_BatchInfo(clsBatchInfo BatchInfo)
        {
            try
            {
                DBCommand cmd = new DBCommand();
                cmd.CommandText = @"update tb_agv_jobinfo set job_start=:job_start, job_end=:job_end where agv_id=:agv_id and job_id=:job_id ";
                cmd.AddParameter(":agv_id", BatchInfo.AGV);
                foreach (clsLotInfo lot in BatchInfo.Lots)
                {
                    cmd.AddParameter(":job_id", lot.Mission);
                    break;
                }
                cmd.AddParameter(":job_start", BatchInfo.StartTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                cmd.AddParameter(":job_end", BatchInfo.EndTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                int result = xdb.ExecuteSql(cmd.CommandText);
            }
            catch (System.Exception ex)
            {	
            }
        }

        private bool Is_Exist_BatchInfo(clsBatchInfo BatchInfo)
        {
            bool result = false;
            try
            {
                DBCommand cmd = new DBCommand();
                cmd.CommandText = @"select * from tb_agv_jobinfo t where t.agv_id=:agv_id and t.job_id=:job_id ";
                cmd.AddParameter(":agv_id", BatchInfo.AGV);
                foreach (clsLotInfo lot in BatchInfo.Lots)
                {
                    cmd.AddParameter(":job_id", lot.Mission);
                    break;
                }
                DataTable xdt = xdb.ExecuteQuery(cmd.CommandText);
                if (xdt.Rows.Count > 0)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (System.Exception ex)
            {	
            }
            return result;
        }

        public void Update_MissionLotInfo(clsLotInfo LotInfo)
        {
            try
            {
            	if (!Is_Exist_LotInfo(LotInfo))
            	{
                    Insert_LotInfo(LotInfo);
            	} 
            	else
            	{
                    Update_LotInfo(LotInfo);
            	}
            }
            catch (System.Exception ex)
            {	
            }
        }

        public void Insert_LotInfo(clsLotInfo LotInfo)
        {
            try
            {
                DBCommand cmd = new DBCommand();
                cmd.CommandText = @"insert into tb_agv_lotinfo (lot_id, job_id, src_tool, src_port, des_tool, des_port, wafer_qty, lot_start, lot_end) values (:lot_id, :job_id, :src_tool, :src_port, :des_tool, :des_port, :wafer_qty, :lot_start, :lot_end) ";
                cmd.AddParameter(":lot_id", LotInfo.LotID);
                cmd.AddParameter(":job_id", LotInfo.Mission);
                if (LotInfo.Src == LotInfo.AGV_EQPPort)
                {
                	cmd.AddParameter(":src_tool", LotInfo.EQP);
                    cmd.AddParameter(":src_port", LotInfo.EQP_Port);
                } 
                else
                {
                    cmd.AddParameter(":src_tool", LotInfo.RackID);
                    cmd.AddParameter(":src_port", LotInfo.RackPosition.ShowID);
                }
                if (LotInfo.Des == LotInfo.AGV_EQPPort)
                {
                    cmd.AddParameter(":des_tool", LotInfo.EQP);
                    cmd.AddParameter(":des_port", LotInfo.EQP_Port);
                }
                else
                {
                    cmd.AddParameter(":des_tool", LotInfo.RackID);
                    cmd.AddParameter(":des_port", LotInfo.RackPosition.ShowID);
                }
                cmd.AddParameter(":wafer_qty", LotInfo.Qty);
                cmd.AddParameter(":lot_start", LotInfo.StartTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                cmd.AddParameter(":lot_end", LotInfo.EndTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                int result = xdb.ExecuteSql(cmd.CommandText);
            }
            catch (System.Exception ex)
            {	
            }
        }

        public void Update_LotInfo(clsLotInfo LotInfo)
        {
            try
            {
                DBCommand cmd = new DBCommand();
                cmd.CommandText = @"update tb_agv_lotinfo set src_tool=:src_tool, src_port=:src_port, des_tool=:des_tool, des_port=:des_port, wafer_qty=:wafer_qty, lot_start=:lot_start, lot_end=:lot_end where lot_id=:lot_id and job_id=:job_id ";
                cmd.AddParameter(":lot_id", LotInfo.LotID);
                cmd.AddParameter(":job_id", LotInfo.Mission);
                if (LotInfo.Src == LotInfo.AGV_EQPPort)
                {
                    cmd.AddParameter(":src_tool", LotInfo.EQP);
                    cmd.AddParameter(":src_port", LotInfo.EQP_Port);
                }
                else
                {
                    cmd.AddParameter(":src_tool", LotInfo.RackID);
                    cmd.AddParameter(":src_port", LotInfo.RackPosition.ShowID);
                }
                if (LotInfo.Des == LotInfo.AGV_EQPPort)
                {
                    cmd.AddParameter(":des_tool", LotInfo.EQP);
                    cmd.AddParameter(":des_port", LotInfo.EQP_Port);
                }
                else
                {
                    cmd.AddParameter(":des_tool", LotInfo.RackID);
                    cmd.AddParameter(":des_port", LotInfo.RackPosition.ShowID);
                }
                cmd.AddParameter(":wafer_qty", LotInfo.Qty);
                cmd.AddParameter(":lot_start", LotInfo.StartTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                cmd.AddParameter(":lot_end", LotInfo.EndTime.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                int result = xdb.ExecuteSql(cmd.CommandText);
            }
            catch (System.Exception ex)
            {	
            }
        }

        private bool Is_Exist_LotInfo(clsLotInfo LotInfo)
        {
            bool result = false;
            try
            {
                DBCommand cmd = new DBCommand();
                cmd.CommandText = @"select * from tb_agv_lotinfo t where t.lot_id=:lot_id and t.job_id=:job_id ";
                cmd.AddParameter(":lot_id", LotInfo.LotID);
                cmd.AddParameter(":job_id", LotInfo.Mission);
                DataTable xdt = xdb.ExecuteQuery(cmd.CommandText);
                if (xdt.Rows.Count > 0)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (System.Exception ex)
            {
            }
            return result;
        }

        public void Update_Mini_DateTime()
        {
            try
            {
                DBCommand cmd = new DBCommand();
                cmd.CommandText = @"update tb_agv_jobinfo set job_end=:act_job_end where job_end=:job_end ";
                cmd.AddParameter(":act_job_end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                cmd.AddParameter(":job_end", DateTime.MinValue.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                int result = xdb.ExecuteSql(cmd.CommandText);

                cmd = new DBCommand();
                cmd.CommandText = @"update tb_agv_lotinfo set lot_end=:act_lot_end where lot_end=:lot_end ";
                cmd.AddParameter(":act_lot_end", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                cmd.AddParameter(":lot_end", DateTime.MinValue.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                result = xdb.ExecuteSql(cmd.CommandText);
            }
            catch (System.Exception ex)
            {	
            }
        }
    }
}
